package com.zimmur.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.HomeMadeExample;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.view.IndexView;
import com.zimmur.service.IHomeMadeService;
import com.zimmur.tools.PageUtil;

@RestController  
@RequestMapping("/restful/homemade")  
public class HomeMadeController extends BaseController {
	private static Logger log=LoggerFactory.getLogger(HomeMadeController.class);
	
	 @Resource(name="homeMadeServiceImpl")
	 private IHomeMadeService homeMadeService;
	 
	 /**
	 * 家定首页
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/index",method=RequestMethod.GET)  
     public ReturnJsonEntity homeMadeIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			long userId= getTokenUserId();
			IndexView indexView = homeMadeService.homeMadeIndex(userId,pageNow);
		    
			entity.setData(indexView);   
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	 /**
	 * 家定首页 分页
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/indexpage",method=RequestMethod.GET)  
     public ReturnJsonEntity homeMadeIndexPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			long userId= getTokenUserId();
			PageUtil<Topic>  page = homeMadeService.homeMadeIndexPage(userId,pageNow);
		    
			entity.setData(page);   
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	 /**
	 * 定制详情
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/madedetail",method=RequestMethod.GET)  
     public ReturnJsonEntity madeDetail(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			long goodsId = getLongParam("goodsId");
			long userId= getTokenUserId();
			
			Goods goods = homeMadeService.madeDetail(goodsId,userId);
			
			entity.setData(goods); 
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	 /**
	 * 定制选项
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/choosefabric",method=RequestMethod.GET)  
     public ReturnJsonEntity chooseFabric(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			long goodsId = getLongParam("goodsId");
			long userId= getTokenUserId();
			
			HomeMadeExample homeMadeExample = homeMadeService.chooseFabric(goodsId,userId);
		    
			entity.setData(homeMadeExample);   
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
}
